package com.example.easypoi.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.easypoi.entity.DigitalElasticElement;
import com.example.easypoi.entity.DigitalPressureGauge;
import com.example.easypoi.mapper.DigitalElasticElementMapper;
import com.example.easypoi.mapper.DigitalPressureGaugeMapper;
import com.example.easypoi.service.ExcelImportService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/excelImport")
@CrossOrigin
public class ExcelImportController {

    @Resource
    private ExcelImportService excelImportService;
    @Resource
    private DigitalPressureGaugeMapper digitalPressureGaugeMapper;

    @Resource
    private DigitalElasticElementMapper digitalElasticElementMapper;

    @ResponseBody
    @PostMapping("importExcel")
    public List<List<Object>> importExcel (@RequestParam("file") MultipartFile file) {

        try {
            List<List<Object>> lists = excelImportService.importFile(file);
            System.out.println(lists.get(0).get(0));
            if (lists.get(0).get(0).toString().equals("数字压力计校准记录")){
            DigitalPressureGauge digitalPressureGauge = new DigitalPressureGauge();
            UUID uuid = UUID.randomUUID();
            digitalPressureGauge.setId(String.valueOf(uuid));
            digitalPressureGauge.setData1001(lists.get(3).get(1).toString());
            digitalPressureGauge.setData1002(lists.get(3).get(3).toString());
            digitalPressureGauge.setData1003(lists.get(3).get(5).toString());
            digitalPressureGauge.setData1004(lists.get(5).get(1).toString());
            digitalPressureGauge.setData1005(lists.get(5).get(2).toString());
            digitalPressureGauge.setData1006(lists.get(5).get(4).toString());
            digitalPressureGauge.setData1007(lists.get(5).get(6).toString());
            digitalPressureGauge.setData1008(lists.get(7).get(1).toString());
            digitalPressureGauge.setData1009(lists.get(7).get(3).toString());
            digitalPressureGauge.setData1010(lists.get(7).get(5).toString());
            digitalPressureGauge.setData1011(lists.get(8).get(3).toString());
            digitalPressureGauge.setData1012(lists.get(9).get(1).toString());
            digitalPressureGauge.setData1013(lists.get(9).get(2).toString());
            digitalPressureGauge.setData1014(lists.get(9).get(3).toString());
            digitalPressureGauge.setData1015(lists.get(12).get(2).toString());
            System.out.println(lists.get(12).get(7));
            digitalPressureGauge.setData1016(lists.get(12).get(7).toString());
            digitalPressureGauge.setData1017(lists.get(13).get(2).toString());
            digitalPressureGauge.setData1018(lists.get(13).get(7).toString());
            digitalPressureGauge.setData1019(lists.get(14).get(2).toString());
            digitalPressureGauge.setData1020(lists.get(14).get(3).toString());
            digitalPressureGauge.setData1021(lists.get(14).get(4).toString());
            digitalPressureGauge.setData1022(lists.get(14).get(5).toString());
            digitalPressureGauge.setData1023(lists.get(14).get(6).toString());
            digitalPressureGauge.setData1029(lists.get(14).get(7).toString());
            digitalPressureGauge.setData1024(lists.get(15).get(2).toString());
            digitalPressureGauge.setData1025(lists.get(15).get(3).toString());
            digitalPressureGauge.setData1026(lists.get(15).get(4).toString());
            digitalPressureGauge.setData1027(lists.get(15).get(5).toString());
            digitalPressureGauge.setData1028(lists.get(15).get(6).toString());
            digitalPressureGauge.setData1030(lists.get(16).get(2).toString());
            digitalPressureGauge.setData1031(lists.get(16).get(7).toString());
            digitalPressureGauge.setData1032(lists.get(17).get(2).toString());
            digitalPressureGauge.setData1033(lists.get(17).get(7).toString());
            digitalPressureGauge.setData1034(lists.get(19).get(7).toString());
            digitalPressureGauge.setData1035(lists.get(23).get(0).toString());
            digitalPressureGauge.setData1036(lists.get(23).get(1).toString());
            digitalPressureGauge.setData1037(lists.get(23).get(2).toString());
            digitalPressureGauge.setData1038(lists.get(23).get(3).toString());
            digitalPressureGauge.setData1039(lists.get(23).get(4).toString());
            digitalPressureGauge.setData1040(lists.get(23).get(5).toString());
            digitalPressureGauge.setData1041(lists.get(23).get(6).toString());
            System.out.println(lists.get(23).get(0).toString());
            digitalPressureGauge.setData1042(lists.get(24).get(0).toString());
            digitalPressureGauge.setData1043(lists.get(24).get(1).toString());
            digitalPressureGauge.setData1044(lists.get(24).get(2).toString());
            digitalPressureGauge.setData1045(lists.get(24).get(3).toString());
            digitalPressureGauge.setData1046(lists.get(24).get(4).toString());
            digitalPressureGauge.setData1047(lists.get(24).get(5).toString());
            digitalPressureGauge.setData1048(lists.get(24).get(6).toString());
            digitalPressureGauge.setData1049(lists.get(25).get(0).toString());
            digitalPressureGauge.setData1050(lists.get(25).get(1).toString());
            digitalPressureGauge.setData1051(lists.get(25).get(2).toString());
            digitalPressureGauge.setData1052(lists.get(25).get(3).toString());
            digitalPressureGauge.setData1053(lists.get(25).get(4).toString());
            digitalPressureGauge.setData1054(lists.get(25).get(5).toString());
            digitalPressureGauge.setData1055(lists.get(25).get(6).toString());
            digitalPressureGauge.setData1056(lists.get(26).get(0).toString());
            digitalPressureGauge.setData1057(lists.get(26).get(1).toString());
            digitalPressureGauge.setData1058(lists.get(26).get(2).toString());
            digitalPressureGauge.setData1059(lists.get(26).get(3).toString());
            digitalPressureGauge.setData1060(lists.get(26).get(4).toString());
            digitalPressureGauge.setData1061(lists.get(26).get(5).toString());
            digitalPressureGauge.setData1062(lists.get(26).get(6).toString());
            digitalPressureGauge.setData1063(lists.get(27).get(0).toString());
            digitalPressureGauge.setData1064(lists.get(27).get(1).toString());
            digitalPressureGauge.setData1065(lists.get(27).get(2).toString());
            digitalPressureGauge.setData1066(lists.get(27).get(3).toString());
            digitalPressureGauge.setData1067(lists.get(27).get(4).toString());
            digitalPressureGauge.setData1068(lists.get(27).get(5).toString());
            digitalPressureGauge.setData1069(lists.get(27).get(6).toString());
            digitalPressureGauge.setData1070(lists.get(35).get(1).toString());
            digitalPressureGauge.setData1071(lists.get(35).get(4).toString());
            digitalPressureGauge.setData1072(lists.get(36).get(1).toString());
            digitalPressureGauge.setData1073(lists.get(36).get(4).toString());
            digitalPressureGauge.setData1074(lists.get(37).get(1).toString());
            digitalPressureGauge.setData1075(lists.get(37).get(4).toString());
            digitalPressureGauge.setData1076(lists.get(38).get(1).toString());
            LocalDateTime now = LocalDateTime.now();
            digitalPressureGauge.setStartTime(now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
            digitalPressureGaugeMapper.insert(digitalPressureGauge);
            }else if (lists.get(0).get(7).toString().equals("记录编号：GC-AS-A-RD-032-R009版本：1.0")){
                DigitalElasticElement digitalElasticElement = new DigitalElasticElement();
                UUID uuid = UUID.randomUUID();
                digitalElasticElement.setId(uuid.toString());
                digitalElasticElement.setData1001(lists.get(2).get(1).toString());
                digitalElasticElement.setData1002(lists.get(2).get(3).toString());
                digitalElasticElement.setData1003(lists.get(2).get(5).toString());
                digitalElasticElement.setData1004(lists.get(2).get(7).toString());
                digitalElasticElement.setData1005(lists.get(4).get(1).toString());
                digitalElasticElement.setData1006(lists.get(4).get(2).toString());
                digitalElasticElement.setData1007(lists.get(4).get(4).toString());
                digitalElasticElement.setData1008(lists.get(4).get(5).toString());
                digitalElasticElement.setData1009(lists.get(6).get(1).toString());
                digitalElasticElement.setData1010(lists.get(6).get(3).toString());
                digitalElasticElement.setData1011(lists.get(6).get(5).toString());
                digitalElasticElement.setData1012(lists.get(8).get(1).toString());
                digitalElasticElement.setData1013(lists.get(8).get(2).toString());
                digitalElasticElement.setData1014(lists.get(11).get(2).toString());
                digitalElasticElement.setData1015(lists.get(12).get(2).toString());
                digitalElasticElement.setData1016(lists.get(13).get(2).toString());
                digitalElasticElement.setData1017(lists.get(18).get(0).toString());
                System.out.println(lists.get(18).get(0));
                digitalElasticElement.setData1018(lists.get(18).get(2).toString());
                digitalElasticElement.setData1019(lists.get(18).get(3).toString());
                digitalElasticElement.setData1020(lists.get(18).get(4).toString());
                digitalElasticElement.setData1021(lists.get(18).get(5).toString());
                digitalElasticElement.setData1022(lists.get(18).get(6).toString());
                digitalElasticElement.setData1023(lists.get(18).get(7).toString());
                digitalElasticElement.setData1024(lists.get(18).get(8).toString());
                digitalElasticElement.setData1025(lists.get(18).get(9).toString());
                digitalElasticElement.setData1026(lists.get(18).get(10).toString());
                digitalElasticElement.setData1027(lists.get(19).get(0).toString());
                digitalElasticElement.setData1028(lists.get(19).get(2).toString());
                digitalElasticElement.setData1029(lists.get(19).get(3).toString());
                digitalElasticElement.setData1030(lists.get(19).get(4).toString());
                digitalElasticElement.setData1031(lists.get(19).get(5).toString());
                digitalElasticElement.setData1032(lists.get(19).get(6).toString());
                digitalElasticElement.setData1033(lists.get(19).get(7).toString());
                digitalElasticElement.setData1034(lists.get(19).get(8).toString());
                digitalElasticElement.setData1035(lists.get(19).get(9).toString());
                digitalElasticElement.setData1036(lists.get(19).get(10).toString());
                digitalElasticElement.setData1037(lists.get(20).get(0).toString());
                digitalElasticElement.setData1038(lists.get(20).get(2).toString());
                digitalElasticElement.setData1039(lists.get(20).get(3).toString());
                digitalElasticElement.setData1040(lists.get(20).get(4).toString());
                digitalElasticElement.setData1041(lists.get(20).get(5).toString());
                digitalElasticElement.setData1042(lists.get(20).get(6).toString());
                digitalElasticElement.setData1043(lists.get(20).get(7).toString());
                digitalElasticElement.setData1044(lists.get(20).get(8).toString());
                digitalElasticElement.setData1045(lists.get(20).get(9).toString());
                digitalElasticElement.setData1046(lists.get(20).get(10).toString());
                digitalElasticElement.setData1047(lists.get(21).get(0).toString());
                digitalElasticElement.setData1048(lists.get(21).get(2).toString());
                digitalElasticElement.setData1049(lists.get(21).get(3).toString());
                digitalElasticElement.setData1050(lists.get(21).get(4).toString());
                digitalElasticElement.setData1051(lists.get(21).get(5).toString());
                digitalElasticElement.setData1052(lists.get(21).get(6).toString());
                digitalElasticElement.setData1053(lists.get(21).get(7).toString());
                digitalElasticElement.setData1054(lists.get(21).get(8).toString());
                digitalElasticElement.setData1055(lists.get(21).get(9).toString());
                digitalElasticElement.setData1056(lists.get(21).get(10).toString());
                digitalElasticElement.setData1057(lists.get(22).get(0).toString());
                digitalElasticElement.setData1058(lists.get(22).get(2).toString());
                digitalElasticElement.setData1059(lists.get(22).get(3).toString());
                digitalElasticElement.setData1060(lists.get(22).get(4).toString());
                digitalElasticElement.setData1061(lists.get(22).get(5).toString());
                digitalElasticElement.setData1062(lists.get(22).get(6).toString());
                digitalElasticElement.setData1063(lists.get(22).get(7).toString());
                digitalElasticElement.setData1064(lists.get(22).get(8).toString());
                digitalElasticElement.setData1065(lists.get(22).get(9).toString());
                digitalElasticElement.setData1066(lists.get(22).get(10).toString());
                digitalElasticElement.setData1067(lists.get(23).get(0).toString());
                digitalElasticElement.setData1068(lists.get(23).get(2).toString());
                digitalElasticElement.setData1069(lists.get(23).get(3).toString());
                digitalElasticElement.setData1070(lists.get(23).get(4).toString());
                digitalElasticElement.setData1071(lists.get(23).get(5).toString());
                digitalElasticElement.setData1072(lists.get(23).get(6).toString());
                digitalElasticElement.setData1073(lists.get(23).get(7).toString());
                digitalElasticElement.setData1074(lists.get(23).get(8).toString());
                digitalElasticElement.setData1075(lists.get(23).get(9).toString());
                digitalElasticElement.setData1076(lists.get(23).get(10).toString());
                digitalElasticElement.setData1077(lists.get(24).get(0).toString());
                digitalElasticElement.setData1078(lists.get(24).get(2).toString());
                digitalElasticElement.setData1079(lists.get(24).get(3).toString());
                digitalElasticElement.setData1080(lists.get(24).get(4).toString());
                digitalElasticElement.setData1081(lists.get(24).get(5).toString());
                digitalElasticElement.setData1082(lists.get(24).get(6).toString());
                digitalElasticElement.setData1083(lists.get(24).get(7).toString());
                digitalElasticElement.setData1084(lists.get(24).get(8).toString());
                digitalElasticElement.setData1085(lists.get(24).get(9).toString());
                digitalElasticElement.setData1086(lists.get(24).get(10).toString());
                digitalElasticElement.setData1087(lists.get(25).get(0).toString());
                digitalElasticElement.setData1088(lists.get(25).get(2).toString());
                digitalElasticElement.setData1089(lists.get(25).get(3).toString());
                digitalElasticElement.setData1090(lists.get(25).get(4).toString());
                digitalElasticElement.setData1091(lists.get(25).get(5).toString());
                digitalElasticElement.setData1092(lists.get(25).get(6).toString());
                digitalElasticElement.setData1093(lists.get(25).get(7).toString());
                digitalElasticElement.setData1094(lists.get(25).get(8).toString());
                digitalElasticElement.setData1095(lists.get(25).get(9).toString());
                digitalElasticElement.setData1096(lists.get(25).get(10).toString());
                System.out.println(lists.get(25).get(10));
                digitalElasticElement.setData1097(lists.get(26).get(0).toString());
                digitalElasticElement.setData1098(lists.get(26).get(2).toString());
                digitalElasticElement.setData1099(lists.get(26).get(3).toString());
                digitalElasticElement.setData1100(lists.get(26).get(4).toString());
                digitalElasticElement.setData1101(lists.get(26).get(5).toString());
                digitalElasticElement.setData1102(lists.get(26).get(6).toString());
                digitalElasticElement.setData1103(lists.get(26).get(7).toString());
                digitalElasticElement.setData1104(lists.get(26).get(8).toString());
                digitalElasticElement.setData1105(lists.get(26).get(9).toString());
                digitalElasticElement.setData1106(lists.get(26).get(10).toString());

                digitalElasticElement.setData1107(lists.get(27).get(0).toString());
                digitalElasticElement.setData1108(lists.get(27).get(2).toString());
                digitalElasticElement.setData1109(lists.get(27).get(3).toString());
                digitalElasticElement.setData1110(lists.get(27).get(4).toString());
                digitalElasticElement.setData1111(lists.get(27).get(5).toString());
                digitalElasticElement.setData1112(lists.get(27).get(6).toString());
                digitalElasticElement.setData1113(lists.get(27).get(7).toString());
                digitalElasticElement.setData1114(lists.get(27).get(8).toString());
                digitalElasticElement.setData1115(lists.get(27).get(9).toString());
                digitalElasticElement.setData1116(lists.get(27).get(10).toString());

                digitalElasticElement.setData1117(lists.get(28).get(0).toString());
                digitalElasticElement.setData1118(lists.get(28).get(2).toString());
                digitalElasticElement.setData1119(lists.get(28).get(3).toString());
                digitalElasticElement.setData1120(lists.get(28).get(4).toString());
                digitalElasticElement.setData1121(lists.get(28).get(5).toString());
                digitalElasticElement.setData1122(lists.get(28).get(6).toString());
                digitalElasticElement.setData1123(lists.get(28).get(7).toString());
                digitalElasticElement.setData1124(lists.get(28).get(8).toString());
                digitalElasticElement.setData1125(lists.get(28).get(9).toString());
                digitalElasticElement.setData1126(lists.get(28).get(10).toString());

                digitalElasticElement.setData1127(lists.get(29).get(0).toString());
                digitalElasticElement.setData1128(lists.get(29).get(2).toString());
                digitalElasticElement.setData1129(lists.get(29).get(3).toString());
                digitalElasticElement.setData1130(lists.get(29).get(4).toString());
                digitalElasticElement.setData1131(lists.get(29).get(5).toString());
                digitalElasticElement.setData1132(lists.get(29).get(6).toString());
                digitalElasticElement.setData1133(lists.get(29).get(7).toString());
                digitalElasticElement.setData1134(lists.get(29).get(8).toString());
                digitalElasticElement.setData1135(lists.get(29).get(9).toString());
                digitalElasticElement.setData1136(lists.get(29).get(10).toString());

                digitalElasticElement.setData1137(lists.get(30).get(0).toString());
                digitalElasticElement.setData1138(lists.get(30).get(2).toString());
                digitalElasticElement.setData1139(lists.get(30).get(3).toString());
                digitalElasticElement.setData1140(lists.get(30).get(4).toString());
                digitalElasticElement.setData1141(lists.get(30).get(5).toString());
                digitalElasticElement.setData1142(lists.get(30).get(6).toString());
                digitalElasticElement.setData1143(lists.get(30).get(7).toString());
                digitalElasticElement.setData1144(lists.get(30).get(8).toString());
                digitalElasticElement.setData1145(lists.get(30).get(9).toString());
                digitalElasticElement.setData1146(lists.get(30).get(10).toString());
                System.out.println(lists.get(30).get(10));
                digitalElasticElement.setData1147(lists.get(31).get(0).toString());
                digitalElasticElement.setData1148(lists.get(35).get(2).toString());
                digitalElasticElement.setData1149(lists.get(35).get(4).toString());
                digitalElasticElement.setData1150(lists.get(35).get(6).toString());
                digitalElasticElement.setData1151(lists.get(35).get(8).toString());
                digitalElasticElement.setData1152(lists.get(35).get(10).toString());
                digitalElasticElement.setData1153(lists.get(35).get(12).toString());
                digitalElasticElement.setData1154(lists.get(36).get(2).toString());
                digitalElasticElement.setData1155(lists.get(36).get(4).toString());
                digitalElasticElement.setData1156(lists.get(36).get(6).toString());
                digitalElasticElement.setData1157(lists.get(36).get(8).toString());
                digitalElasticElement.setData1158(lists.get(36).get(10).toString());
                digitalElasticElement.setData1159(lists.get(36).get(12).toString());
                digitalElasticElement.setData1160(lists.get(37).get(2).toString());
                LocalDateTime now = LocalDateTime.now();
                digitalElasticElement.setStartTime(now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
                digitalElasticElementMapper.insert(digitalElasticElement);
            }
            return lists;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @ResponseBody
    @PostMapping("importExcelElasticity")
    public List<List<Object>> importExcelElasticity (@RequestParam("file") MultipartFile file) {
        List<List<Object>> list = excelImportService.importFile(file);
        return list;
    }


    @ResponseBody
    @GetMapping("getAllDigitalPressureGauge")
    public IPage<DigitalPressureGauge> getAllDigitalPressureGauge(DigitalPressureGauge digitalPressureGauge, Integer pageNumber, Integer pageSize) {
        pageNumber=pageNumber==null?1:(pageNumber<1?1:pageNumber);
        pageSize=pageSize==null?5:(pageSize<5?5:pageSize);
        return excelImportService.getAllDigitalPressureGauge(digitalPressureGauge,pageNumber,pageSize);
    }

    @ResponseBody
    @GetMapping("getAllDigitalElasticElement")
    public IPage<DigitalElasticElement> getAllDigitalElasticElement(DigitalElasticElement digitalElasticElement, Integer pageNumber, Integer pageSize) {
        pageNumber=pageNumber==null?1:(pageNumber<1?1:pageNumber);
        pageSize=pageSize==null?5:(pageSize<5?5:pageSize);
        return excelImportService.getAllDigitalElasticElement(digitalElasticElement,pageNumber,pageSize);
    }

    @ResponseBody
    @GetMapping("getAllData1004")
    public List<DigitalPressureGauge> getAllDate1004() {
        return excelImportService.getAllData1004();
    }

    @ResponseBody
    @GetMapping("getAllData1005")
    public List<DigitalElasticElement> getAllDate1005() {
        return excelImportService.getAllData1005();
    }

}
